/**
 * This code is part of the Bumpslide Library by David Knape
 * http://bumpslide.com/
 * 
 * Copyright (c) 2006, 2007, 2008 by Bumpslide, Inc.
 * 
 * Released under the open-source MIT license.
 * http://www.opensource.org/licenses/mit-license.php
 * see LICENSE.txt for full license terms
 */ 
package 
{
	import com.bumpslide.events.UIEvent;
	import com.bumpslide.ui.Applet;
	import com.bumpslide.ui.FramerateMonitor;
	import com.bumpslide.ui.GenericButton;
	import com.bumpslide.ui.Slider;
	import com.bumpslide.ui.VSlider;
	import com.bumpslide.ui.ZoomPanel;

	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.net.URLRequest;

	/**
	 * Zoom Demo 1 Applet
	 *  
	 * @mxmlc -sp=../../classes -default-background-color 0xffffff -default-frame-rate 60 --use-network=false
	 * @author David Knape
	 */
	public class ZoomDemo_01 extends Applet {

		static private const CONTENT_URL:String = "map.jpg";
		private var zoomPanel:ZoomPanel;
		private var image:Loader;
		private var controls:Sprite;
		private var zoomSlider:VSlider;

		/**
		 * Called during initialization
		 */
		override protected function addChildren():void {
			
			// load image with smoothing applied
			image = new Loader();
			image.contentLoaderInfo.addEventListener( Event.INIT, function(e:Event):void { (image.content as Bitmap).smoothing = true; }  );
			image.load( new URLRequest( CONTENT_URL));
			
			// create zoom panel with image as content
			zoomPanel = new ZoomPanel();			
			zoomPanel.content = image;
			addChild( zoomPanel );
			
			// create sprite to hold controls
			controls = new Sprite();		
			
			// create zoom slider
			zoomSlider = new VSlider();
			zoomSlider.move( 0, 20 );
			zoomSlider.setSize( 18, 100 );
			zoomSlider.maxValue = .25;
			zoomSlider.minValue = 4;
			zoomSlider.value=1;
			zoomSlider.addEventListener( Slider.EVENT_CHANGE, handleSliderChange );
			controls.addChild( zoomSlider );
						controls.addChild( new GenericButton( 70, 18,  20, 0, "Home", function():void { zoomPanel.panTo(0,0); }) );			
			controls.addChild( new GenericButton( 40, 18, 100, 0, "<-", zoomPanel.panLeft ) );
			controls.addChild( new GenericButton( 40, 18, 150, 0, "->",  zoomPanel.panRight ) );			
			controls.addChild( new GenericButton( 40, 18, 200, 0, "/\\",    zoomPanel.panUp ) );			controls.addChild( new GenericButton( 40, 18, 250, 0, "\\/",    zoomPanel.panDown ) );
			
			addChild( controls );			
			
			//zoomPanel.dragZoomControl.minZoom = .5;			//zoomPanel.dragZoomControl.maxZoom = 4;
			
			zoomPanel.zoomContent.cacheAsBitmap = true;
			
			// add a framerate monitor
			addChild( new FramerateMonitor() );			
		}

		private function handleSliderChange(event:UIEvent):void {
			trace('zoomSlider.value: ' + (zoomSlider.value));
			zoomPanel.zoom = zoomSlider.value;
			
		}

		/**
		 * Applets are resized when stage size changes, so re-center content during redraw
		 */
		override protected function draw():void {
			zoomPanel.move( 20, 20 );
			zoomPanel.setSize( width*.7, height*.7 );			
			controls.x = controls.y = 30;
		}
	}
}
